<?php
/* SVN FILE: $Id: permissions.php 259 2010-01-21 17:06:08Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Transfer ACP - permissions
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 259 $
* @lastrevision $Date: 2010-01-21 17:06:08 +0000 (Thu, 21 Jan 2010) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2010-01-21 17:06:08 +0000 (Thu, 21 Jan 2010) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/transfer/adminAction/permissions.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class plugin_admin_transfer_permissions extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->html = marketRegistry::loadTemplate('cp_skin_plugin_transfer');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'transfer_transfer' ) );
		
		$redirect = array(
								'manage' => 'managePermissions',
								'add' => 'addPermission',
								'insert' => 'insertPermission',
								'deny' => 'denyPermission',
								'allow' => 'allowPermission',
								'delete' => 'deletePermission',
							);
		
		if(!$redirect[$this->request['do']])
		{
			$this->request['do'] = 'manage';
		}
		
		$function = $redirect[$this->request['do']];
		$this->$function();
		
		/* Output */
		$this->registry->output->html_main .= $this->registry->output->global_template->global_frame_wrapper();
		$this->registry->output->sendOutput();			
	}
	
	private function managePermissions()
	{
		$this->registry->output->html = $this->html->managePermissions();
	}
	
	private function addPermission()
	{
		$this->registry->output->html = $this->html->addPermissionForm();
	}
	
	private function insertPermission()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		if(!is_array($this->caches['group_cache'][$this->request['group_id']]))
		{
			$this->registry->output->showError(null);
		}
		
		if($this->request['p_option'] != 'anonymous' && $this->request['p_option'] != 'money' && strpos($this->request['p_option'],'item') !== 0)
		{
			$this->registry->output->showError(null);
		}
		
		$this->DB->insert('market_transfer_permissions',array(
														'group_id' => $this->request['group_id'],
														'p_option' => $this->request['p_option'],
														'allowed' => intval($this->request['allowed']) & 1,
						)							);
		
		ipsRegistry::cache()->rebuildCache('ibm_transfer_perm','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['transfer_perm_added_log'] );
		
		$this->registry->output->global_message = $this->lang->words['transfer_perm_added_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=permissions" );
	}
	
	private function denyPermission()
	{
		if(strpos($this->request['option'],'item') === 0)
		{
			$item = explode('-',$this->request['option']);
			if(!is_array($this->caches['ibm_transfer_perm']['allow'][$this->request['group']][$item[0]][$item[1]]))
			{
				$this->registry->output->showError(null);
			}
		}
		else
		{
			if(!is_array($this->caches['ibm_transfer_perm']['allow'][$this->request['group']][$this->request['option']]))
			{
				$this->registry->output->showError(null);
			}
		}
		
		$this->DB->update('market_transfer_permissions',array(
																'allowed' => 0,
		),"group_id=".$this->request['group']." AND p_option='".$this->request['option']."'");
		
		ipsRegistry::cache()->rebuildCache('ibm_transfer_perm','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['transfer_perm_deny_log'] );
		
		$this->registry->output->global_message = $this->lang->words['transfer_perm_deny_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=permissions" );
	}
	
	private function allowPermission()
	{
		if(strpos($this->request['option'],'item') === 0)
		{
			$item = explode('-',$this->request['option']);
			if(!is_array($this->caches['ibm_transfer_perm']['deny'][$this->request['group']][$item[0]][$item[1]]))
			{
				$this->registry->output->showError(null);
			}
		}
		else
		{
			if(!is_array($this->caches['ibm_transfer_perm']['deny'][$this->request['group']][$this->request['option']]))
			{
				$this->registry->output->showError(null);
			}
		}
		
		$this->DB->update('market_transfer_permissions',array(
																'allowed' => 1,
		),"group_id=".$this->request['group']." AND p_option='".$this->request['option']."'");
		
		ipsRegistry::cache()->rebuildCache('ibm_transfer_perm','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['transfer_perm_allow_log'] );
		
		$this->registry->output->global_message = $this->lang->words['transfer_perm_allow_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=permissions" );
	}
	
	private function deletePermission()
	{
		if(strpos($this->request['option'],'item') === 0)
		{
			$item = explode('-',$this->request['option']);
			if(!is_array($this->caches['ibm_transfer_perm']['deny'][$this->request['group']][$item[0]][$item[1]]))
			{
				$this->registry->output->showError(null);
			}
		}
		else
		{
			if(!is_array($this->caches['ibm_transfer_perm']['deny'][$this->request['group']][$this->request['option']]))
			{
				$this->registry->output->showError(null);
			}
		}
		
		$this->DB->delete('market_transfer_permissions',"group_id=".$this->request['group']." AND p_option='".$this->request['option']."'");
		
		ipsRegistry::cache()->rebuildCache('ibm_transfer_perm','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['transfer_perm_delete_log'] );
		
		$this->registry->output->global_message = $this->lang->words['transfer_perm_delete_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=permissions" );
	}
}